package com.example.tool.controller.weather;


import com.alibaba.excel.EasyExcel;
import com.example.tool.bean.weather.WeatherForecastListDTO;
import com.example.tool.bean.weather.WeatherForecastListExportDTO;
import com.example.tool.bean.weather.WeatherForecastWeekExportDTO;
import com.example.tool.bean.weather.WeatherHistoryDataListDTO;
import com.example.tool.bean.weather.WeatherHistoryQueryTempDTO;
import com.example.tool.bean.weather.WeatherHistoryResultVo;
import com.example.tool.service.weather.WeatherService;
import com.example.tool.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

import static com.example.tool.util.ExportUtil.writeExcel;

@Slf4j
@RestController
@RequestMapping("/weather/api")
public class WeatherController {


    @Autowired
    private WeatherService weatherService;

    @GetMapping("/queryHistoryWeather")
    public WeatherHistoryResultVo queryHistoryWeather(@RequestParam(value = "cityId") String cityId,
                                                      @RequestParam(value = "year", required = false) String year,
                                                      @RequestParam(value = "month", required = false) String month,
                                                      @RequestParam(value = "startDate", required = false) String startDate,
                                                      @RequestParam(value = "endDate", required = false) String endDate) {
        return weatherService.queryHistoryWeather(cityId, year, month, startDate, endDate);
    }

    /**
     * 历史天气获取
     *
     * @param file 模板文件
     */
    @PostMapping("/queryHistory/file")
    public void importHistoryWeatherByFile(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        try {
            List<WeatherHistoryQueryTempDTO> queryList = EasyExcel.read(file.getInputStream())
                    .sheet()
                    .head(WeatherHistoryQueryTempDTO.class)
                    .doReadSync();
            List<WeatherHistoryDataListDTO> dataList = weatherService.queryHistoryWeatherBatch(queryList);
            writeExcel(response, dataList, "历史天气.xlsx", "表名", WeatherHistoryDataListDTO.class);
        } catch (Exception e) {
            log.error("导出历史天气异常:", e);
        }
    }

    @GetMapping("/queryForecastWeather")
    public List<WeatherForecastListDTO> queryForecastWeather(@RequestParam(value = "cityId") Integer cityId) {
        return weatherService.queryForecastWeather(cityId);
    }


    /**
     * 预报天气获取
     *
     * @param file 模板文件
     */
    @PostMapping("/queryForecast/file")
    public void exportForecastWeatherByFile(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        try {
            List<WeatherForecastListExportDTO> queryList = EasyExcel.read(file.getInputStream())
                    .sheet()
                    .head(WeatherForecastListExportDTO.class)
                    .doReadSync();
            List<WeatherForecastListExportDTO> dataList = weatherService.queryForecastWeatherBatch(queryList);
            writeExcel(response, dataList, "预报天气.xlsx", "表名", WeatherForecastListExportDTO.class);
        } catch (Exception e) {
            log.error("导出预报天气异常:", e);
        }
    }

    /**
     * 获取全国城市预报天气获取
     */
    @PostMapping("/allForecast/export")
    public void exportAllForecastWeather(HttpServletResponse response) {
        try {
            List<WeatherForecastWeekExportDTO> dataList = weatherService.queryAllForecastWeather();
            writeExcel(response, dataList, "全国城市预报天气" + DateUtil.convertDateToDateStr(new Date()) + ".xlsx", "全国天气", WeatherForecastWeekExportDTO.class);
        } catch (Exception e) {
            log.error("导出全国城市预报天气异常:", e);
        }
    }

}
